import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
public class Taobao {
    static Init init = new Init();
    public static void Spy() throws IOException {
        System.out.println("目标淘宝,全力爬取");
        init.initUnSecureTSL();
        Connection connect = Jsoup.connect(Main.urls[1]);
        Connection conheader;
        int i = Init.userAgents.size();
        Document document = null;
        while(true){
            if (Init.userAgents.isEmpty()){
                System.out.println(i+"条用户标识已耗尽");
                break;
            }
            try{
                conheader = connect.headers(init.Builder(Main.urls[1]));
                document=conheader.get();
            }catch(HttpStatusException e){
                System.out.println("用户标识失效,重新更换标识");
                continue;
            }
            System.out.println("已成功连接淘宝");
            break;
        }
        Elements elements = document.select("ul.service-bd>li>a");
        show(elements);
    }
    static void show(Elements elements){
        int n=0;
        if (elements.isEmpty()){
            System.out.println("淘宝已经穷了");
        }
        else{
            System.out.println("淘宝商品分类:");
            for (Element e : elements){
                if(n++%3==0){
                    System.out.println();
                }
                System.out.print(e.text()+"\t");
            }
            System.out.println();
        }
    }
}
